package com.aistar.controller;

import com.aistar.pojo.ProOrder;
import com.aistar.pojo.Product;
import com.aistar.pojo.Seckill;
import com.aistar.service.ProOrderService;
import com.aistar.service.ProductService;
import com.aistar.service.SecKillService;
import com.aistar.util.DateUtil;
import com.aistar.util.LeeJSONResult;
import com.aistar.util.OrderStatusEnum;
import com.aistar.util.ServerResponse;
import javafx.scene.control.Skin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

@Controller
public class SecKillController {
    @Autowired
    private SecKillService secKillService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProOrderService proOrderService;
    @Autowired
    private ProductService productService;

    @RequestMapping("/seckill/confirmrush")
    @ResponseBody
    public ServerResponse confirmSecKill(String seckillId) {
        int secId = Integer.valueOf(seckillId);
        Seckill seckill = (Seckill) secKillService.getById(Integer.valueOf(seckillId)).getData();
        Long nowTime = new Date().getTime();
        Long startTime = seckill.getStartTime().getTime();
        Long endTime = seckill.getEndTime().getTime();
        System.out.println(redisTemplate.opsForHash().get("recKIll", secId));
        if (nowTime < startTime) {
            return ServerResponse.secKillFailed("活动尚未开始", false);
        }
        if (nowTime > endTime) {
            return ServerResponse.secKillFailed("活动尚已经结束，明天再来吧", false);
        }

//        int secRedisNum = (int) redisTemplate.opsForHash().get("recKill", seckillId);
        int secDBNum = seckill.getSeckillNumber();
        if (secDBNum == 0) {

            return ServerResponse.secKillFailed("商品已抢空，明天再来吧", false);
        } else {
            secDBNum--;
//            String msg = (String) secKillService.secRush(seckill).getData();
            redisTemplate.opsForHash().put("recKill", secId, secDBNum);
            return ServerResponse.getDataSuccess(seckill);
        }


    }

    //秒杀成功  添加订单
    @PostMapping("/secKill/success")
    @ResponseBody
    public LeeJSONResult addOrder(@RequestParam String customeId, @RequestParam String productId, @RequestParam String startDate, @RequestParam String personNumber,
                                  @RequestParam String childNumber, @RequestParam String orderMoney, @RequestParam String orderName, @RequestParam String secKillId) {
        //订单数据处理
        Date endDate = DateUtil.string2Date(startDate);
        Product product = (Product) productService.getById(Integer.valueOf(productId)).getData();
        endDate = DateUtil.getBeforeDate(-(Integer.valueOf(product.getProductDays())));
        ProOrder proOrder = new ProOrder();
        //创建订单对象
        Integer orderId = (Integer) proOrderService.getNewOrderId().getData();
        proOrder.setId(orderId);
        proOrder.setEnddate(endDate);
        proOrder.setCustomerId(Integer.valueOf(customeId));
        proOrder.setProductId(Integer.valueOf(productId));
        proOrder.setChildNumber(Integer.valueOf(childNumber));
        proOrder.setPersonNumber(Integer.valueOf(personNumber));
        proOrder.setOrderMoney(Float.valueOf(orderMoney));
        proOrder.setOrderName(orderName);
        proOrder.setStartdate(DateUtil.string2Date(startDate));
        proOrder.setCreateDate(new Date());
        proOrder.setOrderType("0");
        proOrder.setOrderStatus(OrderStatusEnum.WAIT_PAY.value);
        proOrder.setStatus("1");
        proOrder.setOrderNo(UUID.randomUUID().toString().replace("-", ""));
        System.out.println(proOrder);
        //秒杀成功
        Seckill seckill = (Seckill) secKillService.getById(Integer.valueOf(secKillId)).getData();
        int secNum = seckill.getSeckillNumber();
        secNum--;
        seckill.setSeckillNumber(secNum);
        secKillService.secRush(seckill);
        proOrderService.addOrder(proOrder);
        return LeeJSONResult.ok(orderId);
    }

}
